package com.example.poverty.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.poverty.entity.Towns;
import com.example.poverty.service.TownsService;
import com.example.poverty.utils.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 前端 村镇 控制类
 */
@RestController
@RequestMapping("/towns")
public class TownsController {

    @Autowired
    private TownsService townsService;

    /**
     * 添加村镇信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addTowns(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String tName = request.getParameter("tName").trim();  //村镇名称
        String totalHouse = request.getParameter("totalHouse").trim();  //总户
        String totalNum = request.getParameter("totalNum").trim();  //总人数
        String address = request.getParameter("address").trim();  //地址
        String villageSecretary = request.getParameter("villageSecretary").trim();  //村书记
        String cadreName = request.getParameter("cadreName").trim();  // 驻村干部

        Towns towns = new Towns();
        towns.settName(tName);
        towns.setTotalHouse(Integer.parseInt(totalHouse));
        towns.setTotalNum(Integer.parseInt(totalNum));
        towns.setAddress(address);
        towns.setVillageSecretary(villageSecretary);
        towns.setCadreName(cadreName);
        boolean flag = townsService.insert(towns);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "添加成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "添加失败");
        return jsonObject;
    }

    /**
     * 修改村镇信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateTowns(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String id = request.getParameter("id").trim();  //主键
        String tName = request.getParameter("tName").trim();  //村镇名称
        String totalHouse = request.getParameter("totalHouse").trim();  //总户
        String totalNum = request.getParameter("totalNum").trim();  //总人数
        String address = request.getParameter("address").trim();  //地址
        String villageSecretary = request.getParameter("villageSecretary").trim();  //村书记
        String cadreName = request.getParameter("cadreName").trim();  // 驻村干部


        Towns towns = new Towns();
        towns.setId(Integer.parseInt(id));
        towns.settName(tName);
        towns.setTotalHouse(Integer.parseInt(totalHouse));
        towns.setTotalNum(Integer.parseInt(totalNum));
        towns.setAddress(address);
        towns.setVillageSecretary(villageSecretary);
        towns.setCadreName(cadreName);
        boolean flag = townsService.update(towns);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "添加成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "添加失败");
        return jsonObject;
    }

    /**
     * 删除村镇信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object deleteTowns(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  //主键
        boolean flag = townsService.delete(Integer.parseInt(id));
        return flag;
    }

    /**
     * 根据主键查询整个村镇信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.GET)
    public Object selectByPrimaryKey(HttpServletRequest request) {
        String id = request.getParameter("id").trim();
        return townsService.selectByPrimaryKey(Integer.parseInt(id));
    }

    /**
     * 查找所有村镇信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/allTowns", method = RequestMethod.GET)
    public Object allTowns(HttpServletRequest request) {
        return townsService.allTowns();
    }

    /**
     * 根据村镇用户数查找村镇信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getByTotalHouse", method = RequestMethod.GET)
    public Object getByTotalHouse(HttpServletRequest request) {
        String totalHouse = request.getParameter("totalHouse").trim();
        return townsService.getByTotalHouse(Integer.parseInt(totalHouse));
    }


}
